package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AbolishappFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.AbolishappMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

@Controller
@RequestMapping("equip")
public class AbolishappController extends BaseController{
	@Inject
	private AbolishappMapper abolishapp;
	
	@RequestMapping(value = "equip_abolishapp", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<AbolishappFormMap> findByWhere = abolishapp
				.findByWhere(new AbolishappFormMap());
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("equipbom", findByWhere);
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/abolish/equipbom_list";
	}
	
	@ResponseBody
	@RequestMapping("equip_abolishByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		AbolishappFormMap abolishappFormMap = getFormMap(AbolishappFormMap.class);
		if (abolishappFormMap.get("status")==null) {
			abolishappFormMap.put("status", 1);
		}
		List<Map<String,Object>> equipAbolish = abolishapp.getEquipAbolishapp(abolishappFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipAbolish);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return pageView;			
	}
	
	@ResponseBody
	@RequestMapping(value = "abolish_delEntity")
	@SystemLog(module = "设备报废单", methods = "设备-删除报废单")// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		AbolishappFormMap abolishappFormMap = getFormMap(AbolishappFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		for (int i = 0; i < str.length; i++) {
			abolishappFormMap.set("status", 2);
			abolishappFormMap.put("id", str[i]);
			abolishappFormMap.put("date", sdf.format(date));
			abolishapp.editEntity(abolishappFormMap);
		}
		return "success";
	}
	
	
	@RequestMapping("abolish_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		Map<String, Object> editEquipAbolish = abolishapp.getEditEquipAbolish(id);		
		model.addAttribute("equipabo", editEquipAbolish);
		return Common.BACKGROUND_PATH + "/equip/abolish/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "abolish_editEntity")
	@SystemLog(module = "设备报废单", methods = "报废单-修改报废单")	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		AbolishappFormMap abolishappFormMap = getFormMap(AbolishappFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		abolishappFormMap.put("fillPeopleId", userFormMap.get("id"));
		//需要计算时间工具类
		if (abolishappFormMap.get("usedTime")!=null) {
			Date usedTime = DateUtils.stringToDate(abolishappFormMap.get("usedTime").toString());
			Date installDate = DateUtils.stringToDate(abolishappFormMap.get("installDate").toString());
			long number = (installDate.getTime()-usedTime.getTime())/(1000*3600*24);
			abolishappFormMap.put("usedTime", number+"天");
			abolishappFormMap.remove("installDate");
			abolishappFormMap.put("date", sdf.format(date));
		}		
		abolishapp.editEntity(abolishappFormMap);
		return "success";
	}
	
}
